import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { PoliceEditComponent } from '../police-edit/police-edit.component';
import {ApiService} from "@core/services/api";
import {HttpClient} from "@angular/common/http";
import { ACLService } from '@delon/acl';

@Component({
  selector: 'app-police-list',
  templateUrl: './police-list.component.html',
  styles: []
})
export class PoliceListComponent implements OnInit {
  @ViewChild('st', {static: false})st: TablePageComponent;
  addFlag:boolean = this.aclService.canAbility("police-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("police-manage/edit");
  delFlag:boolean =  this.aclService.canAbility("police-manage/delete");
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '选中',
        type: ColumnType.checkbox
      },
      {
        index: 'field',
        title: '警务区编号',
        filter: {
          type: 'string'
        }
      },
      {
        index: 'policeName',
        title: '警务区名称',
        filter: {
          type: 'string'
        }
      },
      {
        index: 'polickShortName',
        title: '简称',
        filter: {
          type: 'string'
        }
      },
      /*{
        index: 'dmz',
        title: '上级警务区',
        filter: {
          type: 'string'
        }
      },
      {
        index: 'city',
        title: '所属行政区',
        filter: {
          type: 'string'
        }
      },*/
      {
        index: 'policeRank',
        title: '类型',
        type: ColumnType.tag,
        tag: {
          1: { text: '公安局', color: 'green' },
          2: { text: '分局', color: 'red' },
          3: { text: '进行中', color: 'blue' },
          4: { text: '默认', color: '' },
          5: { text: '警告', color: 'orange' },
        },
        filter: {
          type: 'select',
          items: [
            {label: '公安局', value: '1'},
            {label: '分局', value: '2'},
          ]
        }
      },
      {
        index: 'status',
        title: '数据状态',
        filter: {
          type: 'select',
          items: [
            {label: '有效', value: true},
            {label: '无效', value: false},
          ]
        }
      },
      {
        index: 'policeRemark',
        title: '备注'
      }
    ],
    reqUrl: this.api.police.page.url,
    editBtn: [
      {title: '编辑', iif: () => this.editFlag}
    ]
  };
  selectedData: object[];
  editDic(type: 0 | 1 = 0, param?: object): void {
    this.modal.create({
      nzTitle: type === 0 ? '新增警务区' : '修改警务区',
      nzContent: PoliceEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        param
      },
      nzFooter: null
    })
  }
  selectChange(e): void {
    this.selectedData = e;
  }
  operate(e): void {
    console.log(e);
    if (e.title === '编辑') {
      this.editDic(1 , e.data)
    }
  }
  deleteDic(): void {
    if (!this.selectedData || this.selectedData.length === 0) {
      this.msg.warning('请选择数据后操作');
      return;
    }
    this.modal.confirm({
      nzTitle: '您确定要删除这些数据吗',
      nzOnOk: () => {
        const body = [];
        this.selectedData.forEach((item: any) => {
          body.push(item.id);
        })
        this.http.request(this.api.police.delete.method,this.api.police.delete.url,{body}).subscribe((res:any) => {
          if (res.status === '200') {
            this.msg.success('删除成功');
            this.st.reload();
          }
        })
      }
    })
  }
  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private api: ApiService,
    private http: HttpClient,
    private aclService: ACLService
  ) { }


  ngOnInit() {
  }

}
